package com.spring.proxy.gp.test;

import java.lang.reflect.Method;
import java.util.Arrays;

import com.spring.proxy.gp.GPClassLoader;
import com.spring.proxy.gp.GPInvocationHandler;
import com.spring.proxy.gp.GPProxy;

public class UserServiceProxy implements GPInvocationHandler {
	private Object target;

	private UserServiceProxy(Object target) {
		super();
		this.target = target;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getInstance(Object target,Class<? extends T> t) {
		return (T) GPProxy.newProxyInstance(new GPClassLoader(),
				target.getClass().getInterfaces(), new UserServiceProxy(target));
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] arg) throws Throwable {
		Object obj = null;
		before();
		System.out.println(Arrays.asList(arg));
		obj = method.invoke(target, arg);
		after();
		return obj;
	}

	private void after() {
		System.out.println("log after");
	}

	private void before() {
		System.out.println("log before");
	}
}
